home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#41 (Feb 89)
/
Forth code
/
Records.mch
next >
Wrap
Text File
|
1988-12-15
|
5KB
|
194 lines
( THIS IS A SET OF MACHFORTH ROUTINES FOR BUILDING RECORDS)
( THEY ALLOW YOU TO BUILD A NAMED RECORD WITH ITEMS OF VARIOUS SIZES )
( EXECUTING THE RECORD NAME LEAVES THE RECORD SIZE ON THE STACK)
( EXECUTING AN ITEM NAME LEAVES THE OFFSET OF THE ITEM )
( INTO THE RECORD ON THE STACK)
( IT CREATES A TEMPLATE FOR THE RECORD BUT NOT THE ACTUAL RECORD.)
( CREATE THE RECORD WITH " CREATE <NAME> <RECORD NAME> ALLOT")
( OR "VARIABLE <NAME> <RECORD NAME> 4 - VALLOT" DEPENDING IF YOU)
( WANT THE ENTRY IN THE DICTIONARY OR VARIABLE SPACE )
VOCABULARY RECORDS ( NEW VOCABULARY )
ALSO RECORDS
DEFINITIONS
Global
: Align ( n1 -- [n1] or [n1 + 1] makes n word aligned )
dup 2 mod + ; ( USED TO WORD ALIGN 2 & 4 BYTE ITEMS )
Global
: RECORD ( -- a 0)
HERE 4 + CREATE 0 dup W, DOES> W@ ;
( USED TO OPEN A RECORD )
Global
: BYTE: ( a n -- a n1+1)
CREATE DUP W, 1+ DOES> W@ + ;
Global
: BYTES: ( a n1 n2 -- a n1+n2 | AN ARRAY OF n2 bytes )
CREATE OVER Align W, swap Align + DOES> W@ + ;
Global
: SHORT: ( a n1 -- a n1+2 | 2 byte integer item )
CREATE Align DUP W, 2+ DOES> W@ + ;
Global
: WORD: ( a n1 -- a n1+2 | 2 byte integer item )
CREATE Align DUP W, 2+ DOES> W@ + ;
Global
: BOOLEAN: ( a n1 -- a n1+2 | 2 byte boolean item )
CREATE Align DUP W, 2+ DOES> W@ + ;
Global
: SHORTS: ( a n1 n2 -- a n1+n2*2 | an array of n2 shorts )
CREATE OVER Align W, 2* Swap Align + DOES> W@ + ;
Global
: LONG: ( a n1 -- a n1+4 | a 4 byte integer )
CREATE Align DUP W, 4 + DOES> W@ + ;
Global
: POINTER: ( a n1 -- a n1+4 | a 4 byte integer )
CREATE Align DUP W, 4 + DOES> W@ + ;
Global
: LONGS: ( a n1 n2 -- a n1+n2*4 | an array of n2 4 byte integers )
CREATE OVER Align W, 4 * swap Align + DOES> W@ + ;
Global
: HANDLE: ( a n1 -- a n1+4 | a handle, 4 byte, item )
CREATE Align DUP W, 4 + DOES> W@ + ;
Global
: HANDLES: ( a n1 n2 -- a n1+n2*4| array of n2 handles )
CREATE OVER Align W, 4 * swap Align + DOES> W@ + ;
Global
: ADDR: ( a n1 -- a n1+4 | 4 byte address item, ie pointer )
CREATE Align DUP W, 4 + DOES> W@ + ;
Global
: ADDRS: ( a n1 n2 -- a n1+n2*4 | array of n2 addresses )
CREATE OVER Align W, 4 * swap Align + DOES> W@ + ;
Global
: RECT: ( a n1 n2 -- a n1+8 | a rect item )
CREATE Align DUP W, 8 + DOES> W@ + ;
Global
: RECTS: ( a n1 n2 -- a n1+n2*8 | an array of n2 rects )
CREATE OVER Align W, 8 * swap Align + DOES> W@ + ;
Global
: STRING: ( a n1 n2 -- a n1+n2+1 | a string item n2+1 long )
CREATE OVER W, + 1+ DOES> W@ + ;
Global
: RECORD: ( a n1 n2 -- a n1+n2 | a record item of size n2)
CREATE OVER Align W, swap Align + DOES> W@ + ;
Global
: END.RECORD { Mainaddr size --|sets size of struct at a to n }
Mainaddr W@ Size <
IF Size MainAddr W! THen ;
( CLOSES THE RECORD STORES THE RECORD SIZE IN RECORD NAME)
Global
: SUB.REC ( -- )
CReate 0 W, 2DUP Here 2- Rot Rot DOES> W@ ;
( USE TO CREATE A VARIANT RECORD ON THE END OF A RECORD)
Global
: END.SUB { SubAddrs MainAddrs Size -- }
Size SubAddrs W!
MainAddrs W@ Size <
IF Size Align MainAddrs W! THen ;
( USE TO CLOSE VARIANT RECORD )
ONLY MAC
ALSO FORTH
DEFINITIONS
ALSO RECORDS
Global
RECORD Rectangle
Global SHORT: Top
Global Short: Left
Global Short: Bottom
Global SHORT: RIght
ENd.RECORD
Global
: rect Variable Rectangle 4 - VALLOT ;
( CREATES A RECTANGLE RECORD IN THE VARIABLE SPACE )
( --------------------------------------------------------------------)
( USEFUL UTILITIES )
Global
: ," ( --- | create a named string )
34 WORD
COUNT DUP C,
0 DO
DUp i + C@ C,
Loop DRop ;
( USE AFTER YOU "CREATE" <NAME>, A NEW DICTIONARY ENTRY)
( IT WILL CREATE A NAMED STRING UP TO AN ENDING QUOTE MARK)
( EXECUTING <NAME> LEAVES THE ADDRESS OF THE LENGTH BYTE OF STRING)
( ie. Create MyStr ," Hello" )
Global
CODE @@ ( GETS THE ADDRESS OF DATA IN A HANDLE)
MOVE.L (A6)+,A0
MOVE.L (A0),A0
MOVE.L (A0),-(A6)
RTS
END-CODE
Global
CODE @RECT ( DEREFERENCES A RECTANGLE INTO THE 4 PARAMETERS T/L/B/R)
MOVE.L (A6)+,A0
MOVEQ.L #0,D0
MoveQ.L #0,D1
@STB
MOVE.W 0(A0,D1.W),D0
EXT.L D0
MOVE.L D0,-(A6)
ADDQ.W #2,D1
CMPI.W #8,D1
BNE.S @STB
RTS
END-CODE
Global
CODE R><XY ( T/L/B/R >< X1/Y1/X2/Y2 )
MOVEM.L (A6)+,D0-D3
EXG D0,D1
EXG D2,D3
MOVEM.L D0-D3,-(A6)
RTS
END-CODE
Global
CODE <=
MOVEQ.L #0,D0
CMPM.L (A6)+,(A6)+
BGT.S @1
MOVEQ.L #-1,D0
@1 Move.L D0,-(A6)
RTS
END-CODE
Global
CODE >=
MOVEQ.L #0,D0
CMPM.L (A6)+,(A6)+
BLT.S @1
MOVEQ.L #-1,D0
@1 Move.L D0,-(A6)
RTS
END-CODE